package com.yc.scott.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.yc.jdbc.DBHelper.PageDTO;
import com.yc.scott.dao.EmpDao;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class EmpDlg extends JDialog {

	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	private JTextField textField;
	private JTextField textField_1;
	private JTable table;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			EmpDlg dialog = new EmpDlg();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public EmpDlg() {
		setModal(true);
		setBounds(100, 100, 671, 513);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JPanel panel = new JPanel();
			contentPanel.add(panel, BorderLayout.NORTH);
			{
				JLabel lblNewLabel = new JLabel("姓名");
				panel.add(lblNewLabel);
			}
			{
				textField = new JTextField();
				panel.add(textField);
				textField.setColumns(10);
			}
			{
				JLabel lblNewLabel_1 = new JLabel("岗位");
				panel.add(lblNewLabel_1);
			}
			{
				textField_1 = new JTextField();
				panel.add(textField_1);
				textField_1.setColumns(10);
			}
			{
				JButton btnNewButton = new JButton("查询");
				////////////////////////////////////////////////////
				btnNewButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						query();
					}
				});
				panel.add(btnNewButton);
			}
		}
//		{
			JScrollPane scrollPane = new JScrollPane();
			contentPanel.add(scrollPane, BorderLayout.CENTER);
			{
//				table = new JTable();
//				scrollPane.setViewportView(table);
			}
//		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("确定");
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("关闭");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						// 关闭窗口
						EmpDlg.this.dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		
		////////////////////////////////////////////////////////////
		headers.add("编号");
		headers.add("姓名");
		headers.add("岗位");
		headers.add("领导");
		headers.add("入职日期");
		headers.add("工资");
		headers.add("奖金");
		headers.add("岗位");
		headers.add("密码");
		
		// 创建表格
		table = new JTable(data, headers);
		scrollPane.setViewportView(table);
		
		///////////////////////////////
		JPanel pagePanel = new JPanel();
		contentPanel.add(pagePanel, BorderLayout.SOUTH);
		JButton firstBtn = new JButton("第一页");
		JButton preBtn = new JButton("上一页");
		pageLabel = new JLabel("第1页/共2页");
		JButton nextBtn = new JButton("下一页");
		JButton lastBtn = new JButton("最后一页");
		pagePanel.add(firstBtn);
		pagePanel.add(preBtn);
		pagePanel.add(pageLabel);
		pagePanel.add(nextBtn);
		pagePanel.add(lastBtn);
		
		firstBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				page = 1;
				query();
			}
		});
		
		// lambda 表达式 => 匿名类的简写模式 (该匿名类必须是函数接口(只有一个方法的接口))
		preBtn.addActionListener(e -> {
				if(page > 1) page --;
				query();
		});
		
		nextBtn.addActionListener(e -> {
			if(page < maxPage) page ++;
			query();
		});
		
		lastBtn.addActionListener(e -> {
			page = maxPage;
			query();
		});
		
		query();
		
	}
	
	// 数据模型 头+数据
	Vector<String> headers = new Vector<String>();
	Vector<Vector<Object>> data = new Vector<Vector<Object>>();
	
	EmpDao dao = new EmpDao();
	
	int page = 1;
	int size = 5;
	int maxPage = 1;
	JLabel pageLabel;
	
	public void query() {
		
		PageDTO pg = dao.select(textField.getText(), 
				textField_1.getText(), 0, 0, null, null, null, page, size);
		// 清空原有数据
		data.clear();
		pg.getRecords().forEach(map->{
			Vector<Object> row = new Vector<Object>();
			map.forEach((columnName,columnValue)->{
				if("PWD".equals(columnName)) {
					columnValue = "***";
				}
				row.add(columnValue);
			});
			data.add(row);
		});
		
		maxPage = pg.getTotal() / size;
		if(pg.getTotal() % size != 0) {
			maxPage ++;
		}
		
		pageLabel.setText(String.format("第%s页/共%s页", page, maxPage));
		
		// 更新表格控件
		table.updateUI();
		
	}
	
	
	

}
